<?php


namespace app\common\service\coupon\sync_channel;

use app\model\BaseSupplier;
use app\model\CouponCate;
use app\model\CouponBrand;
use app\model\CouponBrandType;
use app\model\CouponProduct;
use app\model\CouponProductChannel;
use app\common\service\coupon\sync_channel\Synchronize;
use think\facade\Log;


abstract class Base
{

    /**
     * 分类
     * @var CouponCate $cates
     */
    public $cates;

    /**
     * 品牌
     * @var CouponBrand $brands
     */
    public $brands;


    /**
     * 品牌分类
     * @var CouponBrandType $brandTypes
     */
    public $brandTypes;

    /**
     * 产品
     * @var CouponProduct $products
     */
    public $products;

    /**
     * 供应商
     * @var BaseSupplier $supplier
     */
    public $supplier;

    /**
     * 产品渠道
     * @var CouponProductChannel $productChannels
     */
    public $productChannels;

    /**
     * 分类处理
     * [[origin_id:1,name:'热门分类']]
     */
    abstract function getCate(): array;


    /**
     *品牌
     * [[origin_id:1,name:'爱奇艺会员']]
     */
    abstract function getBrand();


    /**
     * 具体产品（第一次拉取渠道商的时候处理）
     */
    abstract function getProduct();


    /**
     * 渠道产品（对应的渠道商产品）
     */
    abstract function getChannelProduct();


    /**
     * 类型 黄金会员
     * @return mixed
     */
    abstract function getType();


    /**
     * 保存分类
     */
    public function saveCate()
    {
        /*foreach ($this->cates as &$cate) {
            $cateObj = CouponCate::findOrCreate(['name' => $cate['name'], 'mark' => $cate['origin_id']]);
            $cate['model_id'] = $cateObj->id;
        }*/
    }

    /*
     * 保存品牌
     */
    public function saveBrand()
    {

    }

    /**
     * 保存产品
     */
    public function saveProduct()
    {

    }


    /**
     * 保存品牌
     */
    public function saveType()
    {

    }

    /**
     * 保存渠道产品
     */
    public function saveChannelProduct()
    {
        if (!empty($this->productChannels)) {
            $sync_service = new Synchronize(current($this->productChannels)['type']);
            $used_ids = $sync_service->sync();
            foreach ($this->productChannels as $productChannel) {
                $update = [
                    'name'         => $productChannel['name'],
                    'image'        => $productChannel['image'],
                    'supplier_id'  => $productChannel['supplier_id'],
                    'origin_id'    => $productChannel['origin_id'],
                    'cost_price'   => $productChannel['cost_price'],
                    'market_price' => $productChannel['market_price'],
                    'extend'       => $productChannel['extend'],
                    'type'         => $productChannel['type'],
                    'status'       => $productChannel['status'],
                    'sku_type'     => $productChannel['sku_type'],
                ];
                if ($productChannel['status'] == CouponProductChannel::STATUS_CLOSE) {
                    $update['shelf_state'] = CouponProductChannel::SHELF_STATE_XZ;
                }
                /**
                 *
                 * @var CouponProductChannel $info
                 */
                $info = CouponProductChannel::findOrCreate(
                    //第一个参数是可以确定唯一一条数据的条件
                    [
                        'supplier_id'  => $productChannel['supplier_id'],
                        'origin_id'    => $productChannel['origin_id'],
                    ],
                    //第二个参数需要更新或者写入的数据
                    $update
                );
                unset($used_ids[$info->id]);
            }
            if (!empty($used_ids)) {
                Log::info('拉取不到商品,拉取不到商品信息'.json_encode($used_ids, JSON_UNESCAPED_UNICODE));
                foreach ($used_ids as $id => $origin_id) {
                    /**
                     *
                     * @var CouponProductChannel $info
                     */
                    $info = CouponProductChannel::where('id', $id)->findOrEmpty();
                    $info->status = CouponProductChannel::STATUS_CLOSE;
                    $info->shelf_state = CouponProductChannel::SHELF_STATE_WZ;
                    $info->save();
                }
            }
        }
    }


}